<--- %%NOBANNER%% --> insertrows.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Insert a row to the current table above or below the current line; |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Argument:                                                          |
|    insidedata: if you want to use the function inside a data step; |
|         "T", the function is used inside a step;                   |
|         otherwise, it is used outside a data step;                 |
|         default is outside a data step;                            |
|    position: insert a new row into the table below the current row |
|         or above; default is below;                                |
|         BELOW: insert a row below the current row;                 |
|         ABOVE: insert a row above the current row;                 |
|         default is BELOW;                                          |
|    nofrow - num of rows you want to insert: default is 1;          |
|    wordref: word reference; not necessary default is "wordsys";    |
| Note: the above parameters have the advantages of both positional  |
|       and keyword parameters: as long as they are provided, not    |
|       matter as a keyword or positional parameter, the fucntion    |
|       will know which one is which;                                |
|-------------<-- End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: data _null_; set one; %insertrows(T); run;                |
| Usage: insertrows(insidedata, nofrow=1, wordref=wordsys,           |
|                   position=BELOW);                                 |
\-------------------<-- End of Files Created-->---------------------*/
%macro insertrows(insidedata,nofrow=1, wordref=wordsys,position=BELOW)/parmbuff;
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  2-27-2001 9:44pm;                 |
| Modified: 12-25-2001 4:14am;                |
| Purpose:  Insert a new row;                 |
\--------------------------------------------*/
%local _wcount_ insidedata wsid rc word wordtmp;
%let _wcount_=0;
%do %while(%length(%nrbquote(%scan(&syspbuff, %eval(&_wcount_+1), %str(,())))));
   %let _wcount_=%eval(&_wcount_+1);
   %let word=%qscan(&syspbuff, &_wcount_, %str(,()));
   %if (%index(%quote(&word),%quote(=))) %then %do;
      %let wordtmp=%sysfunc(dequote(%qscan(%quote(&word), 2, %str(=))));
      %if (not %sysfunc(rxmatch(%sysfunc(rxparse($a|$A|.|_)),&wordtmp))) %then %do;
         %let nofrow=&wordtmp;
      %end;
      %else %if (%index(%quote(%upcase(&word)),POSITION)) %then %do;
         %let position=&wordtmp;         
      %end;
      %else %if (%index(%quote(%upcase(&word)),WORD)) or (%index(%quote(%upcase(&word)),WIN)) %then %do;
         %let wordref=&wordtmp;
      %end;
      %else %if (%index(%quote(%upcase(&word)),DATA)) %then %do;
         %let insidedata=&wordtmp;
      %end;
   %end;
   %else %do;
      %if (not %sysfunc(rxmatch(%sysfunc(rxparse($a|$A|.|_)),&word))) %then %do;
         %let nofrow=&word; 
      %end;
      %else %if (%index(%quote(%upcase(&word)),ABOVE) or %index(%quote(%upcase(&word)),BELOW))%then %do;
         %let position=&word;
      %end;
      %else %if (%index(%quote(%upcase(&word)),WORD)) %then %do;
         %let wordref=&word;
      %end;
      %else %if (%index(%quote(%upcase(&word)),T)) or (%index(%quote(%upcase(&word)),F)) %then %do;
         %let insidedata=&word;
      %end;
      %else %let wordref=&word;
   %end;
   %put else nofrow is &nofrow;
%end;
%let wsid=0;
%if (&wordref ne ) %then %do;
   %let wsid=%sysfunc(fopen(&wordref,o,132,e));
%end;
%if &wsid %then %do;
   %let rc=%sysfunc(fclose(&wsid));
   %if (not %index(%quote(%upcase(&insidedata)), T)) %then %do;
   data _null_;
   %end;
   file &wordref lrecl=2000;
   %if (%index(%quote(%upcase(&position)),BELOW)) %then %do;
     str= '[TableInsertRowBelow .NumRows = "'||"&nofrow"||'"]';
     put str;
     %end;
   %else %if (%index(%quote(%upcase(&position)),ABOVE)) %then %do;
     str= '[TableInsertRow .NumRows = "'||"&nofrow"||'"]';
     put str;
     %end;
   %else %do;
     str= '[TableInsertRowBelow .NumRows = "'||"&nofrow"||'"]';
     put str;
     %end;
   put '[CharLeft 1]';
   %if (not %index(%quote(%upcase(&insidedata)), T)) %then %do;
   run;
   %end;
%end;
%else %do;
   %put ==> Alert! Incorrect window reference "&wordref", or window "&wordref" isn%str(%')t open.;
%end;
%mend insertrows;